import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InventoryApi } from '#/models/inventory/inventory';

import { z } from '#/adapter/form';
import { requestClient } from '#/api/request';
import { $t } from '#/locales';
import { op } from '#/utils/permission';

/**
 * 获取搜索表单的字段配置
 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'location_code',
      label: $t('inventory.location.code'),
    },
    {
      component: 'Input',
      fieldName: 'location_name',
      label: $t('inventory.location.name'),
    },
    {
      component: 'ApiSelect',
      fieldName: 'warehouse',
      label: $t('inventory.warehouse.name'),
      rules: z.number().optional(),
      componentProps: {
        allowClear: true, // 使用API动态获取仓库数据，而不是静态字典
        api: () => requestClient.get('/inventory/warehouse/'),
        resultField: 'results', // Django REST framework默认的分页数据字段
        labelField: 'warehouse_name',
        valueField: 'warehouse_id',
      },
    },
    {
      component: 'Select',
      fieldName: 'location_type',
      label: $t('inventory.location.type._value'),
      componentProps: {
        allowClear: true,
        options: [
          {
            label: $t('inventory.location.type.rawMaterial'),
            value: 'raw_material',
          },
          {
            label: $t('inventory.location.type.semiFinished'),
            value: 'semi_finished',
          },
          {
            label: $t('inventory.location.type.finishedProduct'),
            value: 'finished_product',
          },
          {
            label: $t('inventory.location.type.auxiliaryMaterial'),
            value: 'auxiliary_material',
          },
          { label: $t('inventory.location.type.tool'), value: 'tool' },
          { label: $t('inventory.location.type.other'), value: 'other' },
        ],
      },
    },
    {
      component: 'Select',
      fieldName: 'status',
      label: $t('system.status'),
      componentProps: {
        allowClear: true,
        options: [
          {
            label: $t('inventory.location.status.available'),
            value: 'available',
          },
          {
            label: $t('inventory.location.status.unavailable'),
            value: 'unavailable',
          },
          { label: $t('inventory.location.status.locked'), value: 'locked' },
        ],
      },
    },
  ];
}

/**
 * 获取编辑表单的字段配置
 * 根据inventory_location表定义，确保所有字段配置与后端模型一致
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'location_code',
      label: $t('inventory.location.code'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', [$t('inventory.location.code')]))
        .max(
          50,
          $t('ui.formRules.maxLength', [$t('inventory.location.code'), 50]),
        ),
      componentProps: {
        placeholder: $t('inventory.location.code'),
        class: 'w-1/2',
      },
    },
    {
      component: 'Input',
      fieldName: 'location_name',
      label: $t('inventory.location.name'),
      rules: z
        .string()
        .max(
          100,
          $t('ui.formRules.maxLength', [$t('inventory.location.name'), 100]),
        )
        .optional(),
      componentProps: {
        placeholder: $t('inventory.location.name'),
        class: 'w-full',
      },
    },
    {
      component: 'ApiSelect',
      fieldName: 'warehouse',
      label: $t('inventory.warehouse.name'),
      rules: z.number(),
      componentProps: {
        allowClear: false, // 使用API动态获取仓库数据，而不是静态字典
        api: () => requestClient.get('/inventory/warehouse/'),
        resultField: 'results', // Django REST framework默认的分页数据字段
        labelField: 'warehouse_name',
        valueField: 'warehouse_id',
        class: 'w-full',
      },
    },
    {
      component: 'Select',
      fieldName: 'location_type',
      componentProps: {
        placeholder: $t('inventory.location.type._value'),
        options: [
          {
            label: $t('inventory.location.type.rawMaterial'),
            value: 'raw_material',
          },
          {
            label: $t('inventory.location.type.semiFinished'),
            value: 'semi_finished',
          },
          {
            label: $t('inventory.location.type.finishedProduct'),
            value: 'finished_product',
          },
          {
            label: $t('inventory.location.type.auxiliaryMaterial'),
            value: 'auxiliary_material',
          },
          { label: $t('inventory.location.type.tool'), value: 'tool' },
          { label: $t('inventory.location.type.other'), value: 'other' },
        ],
        class: 'w-1/2',
      },
      label: $t('inventory.location.type._value'),
    },
    {
      component: 'Input',
      fieldName: 'zone',
      label: $t('inventory.location.zone'),
      componentProps: {
        placeholder: $t('inventory.location.zone'),
        class: 'w-1/2',
      },
    },
    {
      component: 'Input',
      fieldName: 'row',
      label: $t('inventory.location.row'),
      componentProps: {
        placeholder: $t('inventory.location.row'),
        class: 'w-1/4',
      },
    },
    {
      component: 'Input',
      fieldName: 'column',
      label: $t('inventory.location.column'),
      componentProps: {
        placeholder: $t('inventory.location.column'),
        class: 'w-1/4',
      },
    },
    {
      component: 'Input',
      fieldName: 'level',
      label: $t('inventory.location.level'),
      componentProps: {
        placeholder: $t('inventory.location.level'),
        class: 'w-1/4',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'capacity',
      componentProps: {
        placeholder: $t('inventory.location.capacity'),
        min: 0,
        step: 1,
        class: 'w-1/2',
      },
      label: $t('inventory.location.capacity'),
    },
    {
      component: 'InputNumber',
      fieldName: 'current_load',
      componentProps: {
        placeholder: $t('inventory.location.currentLoad'),
        min: 0,
        step: 1,
        class: 'w-1/2',
        disabled: true, // 当前负载通常由系统自动计算，不允许手动修改
      },
      defaultValue: 0,
      label: $t('inventory.location.currentLoad'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          {
            label: $t('inventory.location.status.available'),
            value: 'available',
          },
          {
            label: $t('inventory.location.status.unavailable'),
            value: 'unavailable',
          },
          { label: $t('inventory.location.status.locked'), value: 'locked' },
        ],
        optionType: 'button',
      },
      defaultValue: 'available',
      fieldName: 'status',
      label: $t('system.status'),
    },
    {
      component: 'Switch',
      fieldName: 'is_aisle',
      label: $t('inventory.location.isAisle'),
      defaultValue: false,
      componentProps: {
        checkedChildren: $t('common.yes'),
        unCheckedChildren: $t('common.no'),
      },
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: $t('system.remark'),
      componentProps: {
        placeholder: $t('system.remark'),
        rows: 3,
        maxLength: 500,
      },
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param actionClick 表格操作按钮点击事件
 */
export function useColumns(
  actionClick?: OnActionClickFn<InventoryApi.Location>,
): VxeTableGridOptions<InventoryApi.Location>['columns'] {
  return [
    {
      title: '序',
      type: 'seq',
      width: 60,
      align: 'center',
      visible: true,
    },
    {
      field: 'location_code',
      title: $t('inventory.location.code'),
      width: 120,
    },
    {
      field: 'location_name',
      title: $t('inventory.location.name'),
      width: 150,
    },
    {
      field: 'warehouse',
      title: $t('inventory.warehouse.name'),
      width: 150,
      formatter: ({ row }: { row: InventoryApi.Location }) =>
        row.warehouse?.warehouse_name || '-',
    },
    {
      field: 'location_type',
      title: $t('inventory.location.type._value'),
      width: 120,
      formatter: ({ row }: { row: InventoryApi.Location }) => {
        const typeMap: Record<string, string> = {
          raw_material: $t('inventory.location.type.rawMaterial'),
          semi_finished: $t('inventory.location.type.semiFinished'),
          finished_product: $t('inventory.location.type.finishedProduct'),
          auxiliary_material: $t('inventory.location.type.auxiliaryMaterial'),
          tool: $t('inventory.location.type.tool'),
          other: $t('inventory.location.type.other'),
        };
        return row.location_type
          ? typeMap[row.location_type] || row.location_type
          : '-';
      },
    },
    {
      field: 'capacity',
      title: $t('inventory.location.capacity'),
      width: 100,
    },
    {
      field: 'current_load',
      title: $t('inventory.location.currentLoad'),
      width: 100,
    },
    {
      field: 'status',
      title: $t('system.status'),
      width: 100,
      formatter: ({ row }: { row: InventoryApi.Location }) => {
        const statusMap: Record<string, string> = {
          available: $t('inventory.location.status.available'),
          unavailable: $t('inventory.location.status.unavailable'),
          locked: $t('inventory.location.status.locked'),
        };
        return row.status ? statusMap[row.status] || row.status : '-';
      },
    },
    {
      field: 'is_aisle',
      title: $t('inventory.location.isAisle'),
      width: 80,
      formatter: ({ row }: { row: InventoryApi.Location }) => {
        return row.is_aisle ? $t('common.yes') : $t('common.no');
      },
    },
    {
      field: 'zone',
      title: $t('inventory.location.zone'),
      width: 80,
    },
    {
      field: 'row',
      title: $t('inventory.location.row'),
      width: 60,
    },
    {
      field: 'column',
      title: $t('inventory.location.column'),
      width: 60,
    },
    {
      field: 'level',
      title: $t('inventory.location.level'),
      width: 60,
    },
    {
      field: 'create_time',
      title: $t('system.createTime'),
      width: 180,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'location_id',
          nameTitle: $t('inventory.location.name'),
          onClick: actionClick,
        },
        name: 'CellOperation',
        options: [
          op('inventory:location:edit', 'edit'),
          op('inventory:location:delete', 'delete'),
        ],
      },
      field: 'action',
      fixed: 'right',
      title: $t('common.other.action'),
      width: 120,
    },
  ];
}
